﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using DoAnForum.Models;
using PagedList;

namespace DoAnForum.Controllers
{
    public class CommentController : Controller
    {
        private static DoAnForumEntities db = new DoAnForumEntities();
        //
        // GET: /Comment/
        [HttpGet]
        [AccessDeniedAuthorize(Roles = "Admin, Mod, Member")]
        public ActionResult CreateComment(int id)
        {
            var cm = new CreateCommentModel {TopicId = id};
            return PartialView("CreateComment",cm);
        }

        [HttpPost]
        [ValidateInput(false)]
        [AccessDeniedAuthorize(Roles = "Admin, Mod, Member")]
        public ActionResult CreateComment(CreateCommentModel model)
        {
            var currUser = db.aspnet_Users.SingleOrDefault(x => x.UserName == User.Identity.Name);
            var cm = new Comment();
            cm.TopicId = model.TopicId;
            cm.CommentBody = model.CmBody;
            cm.CommentCreationDate = cm.CommentLastEditDate= DateTime.Today;
            if (currUser != null)
            {
                cm.UserId = currUser.UserId;
                cm.CommentLastEditUser = currUser.UserId;
            }
            cm.IsStartTopic = false;
            cm.Active = true;

            db.Comments.AddObject(cm);
            db.SaveChanges();
            var topic = db.Topics.SingleOrDefault(x => x.TopicId == model.TopicId);
            topic.TopicLastEditUser = cm.CommentLastEditUser;
            db.SaveChanges();
            var listcm = db.Comments.Where(x => x.TopicId == model.TopicId).OrderBy(x=>x.CommentCreationDate).ToPagedList(1, 10);
            int pageCount = listcm.PageCount;

            return RedirectToAction("ShowTopic","Topic", new {id= model.TopicId, page=pageCount});
        }

        [AccessDeniedAuthorize(Roles = "Admin, Mod")]
        [HttpPost]
        public ContentResult DeleteComment(int id)
        {
            var cm = db.Comments.SingleOrDefault(x=>x.CommentId==id);
            cm.Thanks.ToList().ForEach(x => db.DeleteObject(x));
            db.SaveChanges();
            var topic = db.Topics.SingleOrDefault(x => x.TopicId == cm.TopicId);
            db.Comments.DeleteObject(cm);
            db.SaveChanges();
            var crrUser = db.aspnet_Users.SingleOrDefault(x => User.Identity.Name.Equals(x.UserName));
            topic.TopicLastEditUser = crrUser.UserId;
            topic.TopicLastEditDate = DateTime.Now;
            db.SaveChanges();
            return Content("Bài đã xóa");
        }

        [HttpGet]
        [AccessDeniedAuthorize(Roles = "Admin, Mod, Member")]
        public ActionResult EditComment(int id)
        {
            var cm = db.Comments.SingleOrDefault(x => x.CommentId == id);
            var cmmodel = new CreateCommentModel();
            cmmodel.CmBody = cm.CommentBody;
            cmmodel.TopicId = cm.TopicId;
            Session["CommentId"] = cm.CommentId;
            return View(cmmodel);
        }

        [HttpPost]
        [AccessDeniedAuthorize(Roles = "Admin, Mod, Member")]
        public ActionResult EditComment(CreateCommentModel model)
        {
            int commentId = (int) Session["CommentId"];
            var cm = db.Comments.SingleOrDefault(x => x.CommentId == commentId);
            var currUser = db.aspnet_Users.SingleOrDefault(x => x.UserName == User.Identity.Name);
            cm.CommentLastEditUser = currUser.UserId;
            cm.CommentLastEditDate = DateTime.Now;
            cm.CommentBody = model.CmBody;

            db.SaveChanges();

            return RedirectToAction("ShowTopic","Topic",new {id=cm.TopicId});
        }
    }
}
